import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input, Space, Tag, Typography, Select } from "antd";
// http
import http from '../../../utils/request';

const { Text } = Typography;
const { Option } = Select;



class CloudAlarmSls extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.account_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "集群名称", dataIndex: "cluster_name", key: "cluster_name",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="blue">{rowData.cluster_name}</Tag>
                            </div>
                        )
                    }
                },
                {title: "业务标签", dataIndex: "business", key: "business",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="blue">{rowData.business}</Tag>
                            </div>
                        )
                    }            
                },
                {title: "命名空间", dataIndex: "namespace", key: "namespace"},
                {title: "告警时间", dataIndex: "created", key: "created"},
                {title: "告警类型", dataIndex: "alarm_type", key: "alarm_type"},
                {title: "项目名称", dataIndex: "local_name", key: "local_name"},
                {title: "节点地址", dataIndex: "node_ip", key: "node_ip"},
                {title: "容器IP/名称", dataIndex: "pod_ip", key: "pod_ip", width: '460px',
                    render: (text, rowData) => {
                        return(
                            <Space direction="vertical">
                                <Text >
                                    地址: {rowData.pod_ip}
                                </Text>
                                <Text >
                                    名称: {rowData.pod_name}
                                </Text>
                            </Space>
                        )
                    }
                },
                {title: "重启次数", dataIndex: "restart_count", key: "restart_count"},
                {title: "启动时间", dataIndex: "start_time", key: "start_time"},

            ],
            loading: false,
            search_pod_name: null,
            search_pod_ip: null,
            search_option_business: [],
            search_business_name: null,
        }
    }

    componentDidMount() {
        this.loadData()
        this.loadDataBusiness()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'pod_ip__contains': this.state.search_pod_ip,
            'pod_name__contains': this.state.search_pod_name,
            'business': this.state.search_business_name,
        }

        // 获取分组列表
        http.get('/alarm/sls', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    loadDataBusiness = () => {
        const requestData = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/business', {params: requestData}).then(response=>{
            const search_option_business = []
            const resp_data = response.data.data.items
            for (let i = 0; i < resp_data.length; i++) {
                search_option_business.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
            }
            this.setState({
                search_option_business: search_option_business
            })
        })
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }  

    /** 云平台搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_pod_ip: data.pod_ip ? data.pod_ip: null,
            search_pod_name: data.pod_name ? data.pod_name: null,
        })
        this.loadData()
    }

    // 搜索切换业务线
    onChangeBusiness = (value) => {
        if (value) {
            this.setState({
                search_business_name: value
            })
        } else {
            this.setState({
                search_business_name: null,
            })
        }
    }

    render(){
        const { loading, dataSource, columns, page, total, search_option_business } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="容器IP" name="pod_ip">
                        <Input placeholder="请输入容器IP"  />
                    </Form.Item>
                    <Form.Item label="容器名称" name="pod_name">
                        <Input placeholder="请输入容器名称"  />
                    </Form.Item>
                    <Form.Item label="业务线" name="business_name">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 200 }}
                            placeholder="请选择业务线"
                            optionFilterProp="children"
                            onChange={this.onChangeBusiness}
                            filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {search_option_business}
                        </Select>
                    </Form.Item>

                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}><Button type="primary" onClick={this.onAddCloudPlatformBtn}></Button></div>
                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}
export default CloudAlarmSls;